"use strict"

/**
 * 安徽国家税务局
 */

// 淮北 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4845.html
// 省局 http://www.ah-n-tax.gov.cn/xxgk/510/530/index_4823.html
// 合肥 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4839.html
// 亳州 http://www.ah-n-tax.gov.cn/bzgsww/2015web/bsfw/zfcg/
// 宿州 http://www.ah-n-tax.gov.cn/xxgk/sq/la/
// 蚌埠 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4868.html
// 淮南 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4848.html
// 滁州 http://www.ah-n-tax.gov.cn/czgsww/czgsj2015/xxgk/zfcg/ ====
// 六安 http://www.ah-n-tax.gov.cn/xxgk/sq/la/ ====
// 芜湖 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4862.html
// 宣城 http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4870.html
// 安庆 http://www.ah-n-tax.gov.cn/xxgk/510/530/index_4823.html ====
// 

var cheerio = require("cheerio");
var q = require("q");
var Base = require("./base");
var ahgs_spider = new Base();
ahgs_spider.name = "安徽国家税务局";
ahgs_spider.page = 1;
var g_spider;

var hb_pc = function () { // 淮北爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4845.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var hf_pc = function () { // 合肥爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4839.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var sz_pc = function () { // 宿州爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/sq/la/", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var bb_pc = function () { // 蚌埠爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4868.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var hn_pc = function () { // 淮南爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4848.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var wh_pc = function () { // 芜湖爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4862.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var xc_pc = function () { // 宣城爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/1314/1334/index_4870.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

var sj_pc = function () { // 省局爬虫
    var d = q.defer();
    g_spider.get("http://www.ah-n-tax.gov.cn/xxgk/510/530/index_4823.html", function (data) {
        var $ = cheerio.load(data);
        $("li span a").each(function (index, ele) {
            var e = $(ele);
            var item = {};
            item.title = e.text();
            item.url = "http://www.ah-n-tax.gov.cn/xxgk/" + e.attr("href").substring(6);
            item.source = ahgs_spider.name;
            ahgs_spider.items.push(item);
        });
        d.resolve();
    });
    return d.promise;
};

ahgs_spider.getItemWithPage = function (spider, pg) {
    g_spider = spider;
    return function () {
        var d = q.defer();
        var defer = q('');
        defer.then(hb_pc).then(hf_pc).then(sz_pc).then(bb_pc).then(hn_pc).then(wh_pc).then(xc_pc).then(xc_pc).then(function () {
            console.log(ahgs_spider.items);
            d.resolve();
        });
        return d.promise;
    };
}

exports = module.exports = ahgs_spider;